﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Model;
using Manager;
using ExtAspNet;
using Manager.CustomerHelper;
namespace HealthSystem.sysAdmin
{
    public partial class OrgMana : BasePage
    {
        HW_OrgUserManager manager = new HW_OrgUserManager();
        HW_OrgManager orgManager = new HW_OrgManager();
        public override ModuleType Module
        {
            get
            {
                return ModuleType.OrgManager;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Newbt.OnClientClick = Window2.GetShowReference("Ord_New.aspx","新增机构");
                name.OnClientTriggerClick = Window1.GetSaveStateReference(districtid.ClientID,name.ClientID)
                    + Window1.GetShowReference("~/ChooseDistrict.aspx","选择区域");
                this.actionbt.Text = "";
                BindGrid();
            }
        }
        public void BindGrid()
        {
            int count = 0;
            DataTable list = new DataTable();
            //list = manager.GetSubList(Gird1.PageIndex, Gird1.PageSize, out count, districtId);
           

            if (actionbt.Text == "search")
            {
                Gird1.IsDatabasePaging = false;
                Search(searchbox.Text);
            }
            else
            {
                if (!string.IsNullOrEmpty(districtid.Text))
                {
                    list = orgManager.GetSubList(Gird1.PageIndex, Gird1.PageSize, out count, districtid.Text.Trim());
                    //BindGrid(districtid.Text.Trim());
                }
                else
                {
                    list = orgManager.GetSubList(Gird1.PageIndex, Gird1.PageSize, out count, CustomerProfile.DistrictId);
                    //BindGrid(CustomerProfile.DistrictId);
                }
                Gird1.RecordCount = count;
                Gird1.DataSource = list;
                Gird1.DataBind();
            }
        }
        public void Grid1_DoubleClick(object sender, GridRowClickEventArgs e)
        {
            string orgcode = Gird1.Rows[e.RowIndex].DataKeys[0] as string;
            string orglname = Gird1.Rows[e.RowIndex].DataKeys[1] as string;
            string url = "orgsingle.aspx?orgcode=" + orgcode;
            PageContext.RegisterStartupScript(Window2.GetShowReference(url, orglname + "用户"));
        }
        public void Window1_Close(object sender, ExtAspNet.WindowCloseEventArgs e)
        {
            if (!string.IsNullOrEmpty(districtid.Text))
            {
                Gird1.PageIndex = 0;
                actionbt.Text = "";
                BindGrid();
            }
        }
        public void Window2_Close(object sender, ExtAspNet.WindowCloseEventArgs e)
        {
            
                Gird1.PageIndex = 0;
                BindGrid();
        }
        public void PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Gird1.PageIndex = e.NewPageIndex;
            try
            {
                BindGrid();
                //if (actionbt.Text == "search")
                //{
                //    Gird1.IsDatabasePaging = false;
                //}
                //else
                //{
                //    if (!string.IsNullOrEmpty(districtid.Text))
                //    {
                //        BindGrid(districtid.Text.Trim());
                //    }
                //    else
                //    {
                //        BindGrid(CustomerProfile.DistrictId);
                //    }
                //}
            }
            catch
            {
                Alert.Show("数据错误");
            }
        }
        public void Search_Clik(object sender, EventArgs e)
        {
            Gird1.PageIndex = 0;
            Search(searchbox.Text);
        }
        public void Search(string searchString)
        {
            //if (string.IsNullOrEmpty(searchString))
            //{
            //    ExtAspNet.Alert.Show("请输入查询条件");
            //    return;
            //}
            DataTable dt = orgManager.Search(CustomerProfile.DistrictId, searchString);
            Gird1.IsDatabasePaging = false;
            this.Gird1.DataSource = dt;
            this.Gird1.DataBind();
            actionbt.Text = "search";
        }
        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            try
            {
               // string username = Gird1.Rows[e.RowIndex].DataKeys[0].ToString();
                string orgcode = Gird1.Rows[e.RowIndex].DataKeys[0].ToString();
                if (e.CommandName == "delete")
                {
                    orgManager.DeleteById(orgcode);
                    BindGrid();

                }
                if (e.CommandName == "reset")
                {
                    //重置密码
                    //HW_OrgUserManager orgUserManager = new HW_OrgUserManager();
                    //HW_OrgUser orgUser = orgUserManager.GetById(username);
                    //orgUser.XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    //orgUserManager.ResetPassWord(orgUser);
                    //Alert.Show("密码重置成功");
                    //BindGrid();
                }
            }
            catch
            {
                Alert.Show("操作失败!");
            }
        }
    }
}